package com.dailyblue.java.project.bookstore.alipay.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.dailyblue.java.project.bookstore.alipay.bean.AlipayBean;
import com.dailyblue.java.project.bookstore.alipay.util.JsonResult;
import com.dailyblue.java.project.bookstore.alipay.util.ResultTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

/*
https://opendocs.alipay.com/common/02kipl
https://open.alipay.com/develop/sandbox/app
 */
@RestController
@RequestMapping("/alipay")
@Slf4j
@CrossOrigin
public class AlipayController {

    //获取配置文件中的配置信息
    //应用ID,您的APPID，收款账号既是您的APPID对应支付宝账号
    @Value("${appId}")
    private String appId;

    //商户私钥 您的PKCS8格式RSA2私钥
    @Value("${privateKey}")
    private String privateKey;

    //支付宝公钥
    @Value("${publicKey}")
    private String publicKey;

    //服务器异步通知页面路径
    @Value("${notifyUrl}")
    private String notifyUrl;

    //页面跳转同步通知页面路径
    @Value("${returnUrl}")
    private String returnUrl;

    //签名方式
    @Value("${signType}")
    private String signType;

    //字符编码格式
    @Value("${charset}")
    private String charset;

    //支付宝网关
    @Value("${gatewayUrl}")
    private String gatewayUrl;

    private final String format = "json";

    //PC网页段支付，返回的是支付宝账号的登录页面
    @RequestMapping("/pay/{no}/{subject}/{total}/{body}")
    public String pay(@PathVariable("no") String no, @PathVariable("subject") String subject, @PathVariable("total") String total, @PathVariable("body") String body) throws AlipayApiException {
        AlipayBean alipayBean = new AlipayBean(no, subject, total, body);
        log.info("appId:{}", appId);
        /*
            产生AlipayClient客户端，需要传递的参数分别是：
            支付宝网关、appId、商户私钥、格式、编码集、支付宝公钥和签名方式
         */
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl, appId, privateKey, format, charset, publicKey, signType);
        //PC网页支付使用AlipayTradePagePayRequest传参，下面调用的是pageExecute方法
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        // 设置付款成功后应跳转的路径
        alipayRequest.setReturnUrl(returnUrl);
        alipayRequest.setNotifyUrl(notifyUrl);
        // 设置支付宝的参数：订单号、订单名、金额和描述
        alipayRequest.setBizContent(JSONArray.toJSONString(alipayBean));
        log.info("封装请求支付宝付款参数为:{}", JSONArray.toJSONString(alipayRequest));

        // 调用SDK生成表单
        String result = alipayClient.pageExecute(alipayRequest).getBody();
        log.info("请求支付宝付款返回参数为:{}", result);
        return result;
    }

    @RequestMapping("/success")
    public JsonResult success() {
        return ResultTool.success("交易成功！");
    }

    @RequestMapping(value = "/index")
    public String payCoin() {
        return "index.html";
    }
}

